﻿/*
 * Created by SharpDevelop.
 * User: Zoila
 * Date: 10/11/2011
 * Time: 02:41 a.m.
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using Business_Entity;
using Business_Logic;

namespace PetSoft.GUI.Configuracion
{
	/// <summary>
	/// Description of GUICaja.
	/// </summary>
	public partial class GUICaja : Form
	{
		BETienda tienda;
		List<BECaja> cajaLIST;
		BLCaja blCaja = new BLCaja();
		public GUICaja(BETienda tienda)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			this.tienda=tienda;
		}
		
		void GUICajaLoad(object sender, EventArgs e)
		{
			this.tiendaTB.Text=tienda.ToString();
			this.cajaDGV.RowHeadersVisible=false;
			llenarGrilla();
		}
		public void llenarGrilla(){
			int i=0;
			cajaLIST=blCaja.getCajasByTienda(tienda);
			foreach (BECaja caja in cajaLIST){
				object[] row0={false,caja.IdCaja,caja.Nombre,caja.Estado.Descripcion,i++};
        		this.cajaDGV.Rows.Add(row0);
			}
			
			foreach (DataGridViewRow fila in cajaDGV.Rows){
				if (fila.Cells[2].ToString().Equals("APERTURADA")){
					fila.Cells[0].ReadOnly=true;
				}
				else{
					fila.Cells[0].ReadOnly=false;
				}
			}
			
		}
		
		void NuevoBTNClick(object sender, EventArgs e)
		{
			GUINuevaCaja nueva= new GUINuevaCaja(tienda);
			nueva.ShowDialog();
			cajaDGV.Rows.Clear();
			this.llenarGrilla();
		}
		
		void CajaDGVCellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			
		}
		
		void EliminarBTNClick(object sender, EventArgs e)
		{
			List<BECaja> lista= new List<BECaja>();
			int i=0,contador=0;
			foreach (DataGridViewRow fila in cajaDGV.Rows){
				if (fila.Cells[0].Value!= null && (bool)fila.Cells[0].Value){
					if ( cajaLIST[contador].Estado.IdEstado !="APE") {
						lista.Add(cajaLIST[contador]);//Int32.Parse(fila.Cells[0].ToString())]);
					}
					else
						Utils.MessageInformation("Primero debe cerrar la caja "+cajaLIST[contador].Nombre);
				}
				contador++;
			}
			
			if (contador==0) {
				MessageBox.Show("No ha seleccionado ninguna Caja");
			}
			else {
				try {
					foreach (BECaja caja in lista){
						blCaja.EliminaCaja (caja);
					}
				}
				catch (Exception ex){
					MessageBox.Show(ex.Message,"Error");
				}
				
			}
			cajaDGV.Rows.Clear();
			llenarGrilla();
		}
		
		void CheckBox1CheckedChanged(object sender, EventArgs e)
		{
			
        	if(cajaLIST != null && cajaLIST.Count != 0){
	        	if(checkBox1.Checked)	        			        	
					foreach (DataGridViewRow fila in  cajaDGV.Rows)
							fila.Cells[0].Value = (bool)true;						        			        
	        	if(!checkBox1.Checked)	        			        	
	        		foreach (DataGridViewRow fila in cajaDGV.Rows)					
							fila.Cells[0].Value = (bool)false;						        
        	}else{
        		//MessageBox.Show("No hay elementos para seleccionar","Aviso");
        	}	
		}
		
		void CajaDGVDoubleClick(object sender, EventArgs e)
		{
			
		}
		
		void CajaDGVCellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			int i = Utils.getSelectedIndex(cajaDGV);
			
			if (i != -1){
				if (cajaDGV.Rows[i].Cells[0].Value == null){
					cajaDGV.Rows[i].Cells[0].Value = false;
				}
				cajaDGV.Rows[i].Cells[0].Value = !((bool)cajaDGV.Rows[i].Cells[0].Value);
			}
		}
	}
}
